/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 sequence_loader.cpp

	$Header: /heroes4/sequence_loader.cpp $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "sequence_loader.h"

t_sequence_loader::~t_sequence_loader()
{
}

// ------------------------------------------------------------------------
// thread which attempts to pre-load all of a set of combat actor sequences
// ------------------------------------------------------------------------
void t_sequence_loader::add( t_actor_sequence_cache const& cache )
{
	t_data_locker lock( m_cache_lock );

	m_caches.push_back( cache );
	m_data_available.set();
}

// ------------------------------------------------------------------------
// thread which attempts to pre-load all of a set of combat actor sequences
// ------------------------------------------------------------------------
void t_sequence_loader::clear()
{
	t_data_locker lock( m_cache_lock );
	t_data_locker pointer_lock( m_pointer_lock );

	m_caches.clear();
	m_pointers.clear();
}

// ------------------------------------------------------------------------
// thread which attempts to pre-load all of a set of combat actor sequences
// ------------------------------------------------------------------------
t_uint32 t_sequence_loader::run()
{
	while (!stop_requested())
	{
		// wait up to 1 second while waiting for either a stop request or a new
		// cache to be added to the load list
		if (m_data_available.wait(100))
		{
			// loop until we have emptied the cache list
			while (true)
			{
				t_actor_sequence_cache cache;

				m_cache_lock.lock();
				if (m_caches.empty())
				{
					m_cache_lock.unlock();
					break;
				}
				cache = m_caches.front();
				m_caches.pop_front();
				m_cache_lock.unlock();
				
				// perform the actual load
				t_actor_sequence_ptr pointer;

				pointer = cache.get();

				// save the result
				m_pointer_lock.lock();
				m_pointers.push_back( pointer );
				m_pointer_lock.unlock();
			}
		}
	}
	return 0;
}

